<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JSON格式解析器</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <script src="https://cdn.jsdelivr.net/npm/monaco-editor@0.45.0/min/vs/loader.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        body {
            background: linear-gradient(135deg, #1e3c72, #2a5298);
            min-height: 100vh;
            padding: 20px;
            color: #333;
        }
        
        .container {
            max-width: 1400px;
            margin: 0 auto;
        }
        
        header {
            text-align: center;
            padding: 25px 0;
            margin-bottom: 30px;
            color: white;
        }
        
        h1 {
            font-size: 2.8rem;
            margin-bottom: 15px;
            text-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
        }
        
        .subtitle {
            font-size: 1.2rem;
            opacity: 0.9;
            max-width: 800px;
            margin: 0 auto;
            line-height: 1.6;
        }
        
        .content {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 25px;
            margin-bottom: 30px;
        }
        
        @media (max-width: 1000px) {
            .content {
                grid-template-columns: 1fr;
            }
        }
        
        .panel {
            background: white;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.25);
            overflow: hidden;
            display: flex;
            flex-direction: column;
            height: 500px;
        }
        
        .panel-header {
            background: linear-gradient(90deg, #4b6cb7, #182848);
            color: white;
            padding: 15px 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            font-weight: 600;
        }
        
        .editor-container {
            flex: 1;
            padding: 10px;
        }
        
        #editor {
            width: 100%;
            height: 100%;
            border-radius: 8px;
            overflow: hidden;
        }
        
        #json-tree {
            padding: 15px;
            overflow-y: auto;
            background: #f8f9fa;
            height: 100%;
        }
        
        .controls {
            display: flex;
            gap: 15px;
            padding: 15px 0;
            justify-content: center;
        }
        
        .btn {
            background: linear-gradient(90deg, #4b6cb7, #182848);
            color: white;
            border: none;
            padding: 12px 25px;
            border-radius: 50px;
            font-size: 1rem;
            font-weight: 600;
            cursor: pointer;
            display: flex;
            align-items: center;
            gap: 10px;
            transition: all 0.3s ease;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
        }
        
        .btn:hover {
            transform: translateY(-3px);
            box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
        }
        
        .btn:active {
            transform: translateY(0);
        }
        
        .btn-green {
            background: linear-gradient(90deg, #11998e, #38ef7d);
        }
        
        .btn-red {
            background: linear-gradient(90deg, #f85032, #e73827);
        }
        
        .features {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 25px;
            margin: 40px 0;
        }
        
        .feature-card {
            background: rgba(255, 255, 255, 0.92);
            border-radius: 12px;
            padding: 25px;
            box-shadow: 0 5px 20px rgba(0, 0, 0, 0.15);
        }
        
        .feature-card h3 {
            color: #1e3c72;
            margin-bottom: 15px;
            font-size: 1.4rem;
            display: flex;
            align-items: center;
            gap: 12px;
        }
        
        .feature-card i {
            background: linear-gradient(90deg, #4b6cb7, #182848);
            width: 36px;
            height: 36px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
        }
        
        .feature-card p {
            line-height: 1.7;
            color: #444;
        }
        
        footer {
            text-align: center;
            color: rgba(255, 255, 255, 0.8);
            padding: 25px;
            font-size: 0.95rem;
        }
        
        .tree-item {
            padding: 8px 15px;
            border-left: 2px solid #4b6cb7;
            margin-bottom: 8px;
            background: white;
            border-radius: 4px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
        }
        
        .key {
            color: #1e3c72;
            font-weight: 600;
        }
        
        .string {
            color: #d35400;
        }
        
        .number {
            color: #27ae60;
        }
        
        .boolean {
            color: #8e44ad;
        }
        
        .null {
            color: #7f8c8d;
        }
        
        .error {
            color: #e74c3c;
            background: #fdecea;
            padding: 15px;
            border-radius: 8px;
            margin: 15px;
            font-weight: 500;
        }
        
        .collapsible {
            cursor: pointer;
            position: relative;
            padding-left: 20px;
        }
        
        .collapsible::before {
            content: '-';
            position: absolute;
            left: 0;
            top: 0;
            font-weight: bold;
        }
        
        .collapsed::before {
            content: '+';
        }
        
        .collapsed + .children {
            display: none;
        }
        
        .children {
            padding-left: 25px;
            border-left: 1px dashed #ddd;
            margin: 10px 0 10px 10px;
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1><i class="fas fa-code"></i> JSON格式解析器</h1>
            <p class="subtitle">基于Monaco Editor的强大JSON解析工具，支持语法高亮、自动补全、格式化、错误提示等功能，无JSON长度限制</p>
        </header>
        
        <div class="controls">
            <button class="btn btn-green" id="formatBtn">
                <i class="fas fa-indent"></i>格式化JSON
            </button>
            <button class="btn" id="minifyBtn">
                <i class="fas fa-compress-alt"></i>压缩JSON
            </button>
            <button class="btn" id="validateBtn">
                <i class="fas fa-check-circle"></i>验证JSON
            </button>
            <button class="btn btn-red" id="clearBtn">
                <i class="fas fa-trash-alt"></i>清空内容
            </button>
        </div>
        
        <div class="content">
            <div class="panel">
                <div class="panel-header">
                    <span><i class="fas fa-edit"></i> JSON 编辑器</span>
                    <span id="status">状态: 就绪</span>
                </div>
                <div class="editor-container">
                    <div id="editor"></div>
                </div>
            </div>
            
            <div class="panel">
                <div class="panel-header">
                    <span><i class="fas fa-project-diagram"></i> JSON 结构树</span>
                    <span id="lineCount">0 行</span>
                </div>
                <div id="json-tree"></div>
            </div>
        </div>
        
        <div class="features">
            <div class="feature-card">
                <h3><i class="fas fa-brush"></i> 语法高亮</h3>
                <p>智能识别JSON语法结构，对键、字符串、数字、布尔值等使用不同颜色高亮显示，使JSON结构一目了然。</p>
            </div>
            
            <div class="feature-card">
                <h3><i class="fas fa-lightbulb"></i> 自动补全</h3>
                <p>输入时提供智能提示，自动补全JSON语法元素。Monaco Editor的智能感知功能会根据上下文提供建议。</p>
            </div>
            
            <div class="feature-card">
                <h3><i class="fas fa-exclamation-triangle"></i> 错误提示</h3>
                <p>实时检测JSON语法错误，定位问题位置并提供详细的错误信息。鼠标悬停在错误位置可查看详细说明。</p>
            </div>
            
            <div class="feature-card">
                <h3><i class="fas fa-infinity"></i> 无长度限制</h3>
                <p>支持处理任意大小的JSON文件，无论是小型配置文件还是大型数据集，都能流畅解析和显示。</p>
            </div>
        </div>
        
        <footer>
            <p>© 2023 JSON格式解析器 | 基于Monaco Editor构建 | 支持所有现代浏览器</p>
        </footer>
    </div>
    
    <script>
        // 初始化Monaco Editor
        require.config({ paths: { 'vs': 'https://cdn.jsdelivr.net/npm/monaco-editor@0.45.0/min/vs' }});
        
        require(['vs/editor/editor.main'], function() {
            // 示例JSON数据
            const sampleJson = JSON.stringify({
                appName: "JSON解析器",
                version: "1.0.0",
                features: [
                    "语法高亮",
                    "自动补全",
                    "错误提示",
                    "格式化",
                    "结构树展示"
                ],
                author: {
                    name: "开发者",
                    email: "dev@example.com",
                    active: true
                },
                metadata: {
                    createdAt: "2023-06-15",
                    updatedAt: "2023-09-22",
                    size: "unlimited"
                },
                stats: {
                    lines: 0,
                    characters: 0,
                    objects: 0,
                    arrays: 0
                }
            }, null, 2);
            
            // 创建编辑器
            const editor = monaco.editor.create(document.getElementById('editor'), {
                value: sampleJson,
                language: 'json',
                theme: 'vs-dark',
                fontSize: 14,
                automaticLayout: true,
                minimap: { enabled: true },
                scrollBeyondLastLine: false,
                lineNumbers: 'on',
                roundedSelection: false,
                scrollbar: {
                    vertical: 'auto',
                    horizontal: 'auto'
                }
            });
            
            // 获取DOM元素
            const formatBtn = document.getElementById('formatBtn');
            const minifyBtn = document.getElementById('minifyBtn');
            const validateBtn = document.getElementById('validateBtn');
            const clearBtn = document.getElementById('clearBtn');
            const statusEl = document.getElementById('status');
            const lineCountEl = document.getElementById('lineCount');
            const jsonTreeEl = document.getElementById('json-tree');
            
            // 格式化JSON
            formatBtn.addEventListener('click', function() {
                try {
                    const jsonValue = editor.getValue();
                    const parsed = JSON.parse(jsonValue);
                    const formatted = JSON.stringify(parsed, null, 2);
                    editor.setValue(formatted);
                    statusEl.textContent = "状态: 已格式化";
                    renderJsonTree(parsed);
                } catch (error) {
                    statusEl.textContent = `状态: 错误 - ${error.message}`;
                    jsonTreeEl.innerHTML = `<div class="error">JSON解析错误: ${error.message}</div>`;
                }
            });
            
            // 压缩JSON
            minifyBtn.addEventListener('click', function() {
                try {
                    const jsonValue = editor.getValue();
                    const parsed = JSON.parse(jsonValue);
                    const minified = JSON.stringify(parsed);
                    editor.setValue(minified);
                    statusEl.textContent = "状态: 已压缩";
                    renderJsonTree(parsed);
                } catch (error) {
                    statusEl.textContent = `状态: 错误 - ${error.message}`;
                    jsonTreeEl.innerHTML = `<div class="error">JSON解析错误: ${error.message}</div>`;
                }
            });
            
            // 验证JSON
            validateBtn.addEventListener('click', function() {
                try {
                    const jsonValue = editor.getValue();
                    JSON.parse(jsonValue);
                    statusEl.textContent = "状态: 验证通过 - 有效JSON";
                    jsonTreeEl.innerHTML = '<div class="success"><i class="fas fa-check-circle"></i> JSON格式有效</div>';
                    renderJsonTree(JSON.parse(jsonValue));
                } catch (error) {
                    statusEl.textContent = `状态: 错误 - ${error.message}`;
                    jsonTreeEl.innerHTML = `<div class="error">JSON解析错误: ${error.message}</div>`;
                }
            });
            
            // 清空内容
            clearBtn.addEventListener('click', function() {
                editor.setValue('');
                statusEl.textContent = "状态: 已清空";
                jsonTreeEl.innerHTML = '';
                lineCountEl.textContent = "0 行";
            });
            
            // 渲染JSON树
            function renderJsonTree(data) {
                jsonTreeEl.innerHTML = '';
                
                // 更新行数
                const lineCount = editor.getValue().split('\n').length;
                lineCountEl.textContent = `${lineCount} 行`;
                
                const renderItem = (key, value, depth = 0) => {
                    const itemEl = document.createElement('div');
                    itemEl.className = 'tree-item';
                    
                    if (key !== null) {
                        const keyEl = document.createElement('span');
                        keyEl.className = 'key';
                        keyEl.textContent = key + ': ';
                        itemEl.appendChild(keyEl);
                    }
                    
                    if (Array.isArray(value)) {
                        const collapser = document.createElement('span');
                        collapser.className = 'collapsible';
                        collapser.textContent = `数组[${value.length}]`;
                        itemEl.appendChild(collapser);
                        
                        const childrenEl = document.createElement('div');
                        childrenEl.className = 'children';
                        
                        value.forEach((item, index) => {
                            childrenEl.appendChild(renderItem(null, item, depth + 1));
                        });
                        
                        itemEl.appendChild(childrenEl);
                        
                        collapser.addEventListener('click', function() {
                            collapser.classList.toggle('collapsed');
                        });
                    } else if (value && typeof value === 'object') {
                        const collapser = document.createElement('span');
                        collapser.className = 'collapsible';
                        collapser.textContent = `对象{${Object.keys(value).length}}`;
                        itemEl.appendChild(collapser);
                        
                        const childrenEl = document.createElement('div');
                        childrenEl.className = 'children';
                        
                        Object.entries(value).forEach(([childKey, childValue]) => {
                            childrenEl.appendChild(renderItem(childKey, childValue, depth + 1));
                        });
                        
                        itemEl.appendChild(childrenEl);
                        
                        collapser.addEventListener('click', function() {
                            collapser.classList.toggle('collapsed');
                        });
                    } else {
                        let valueEl = document.createElement('span');
                        
                        if (typeof value === 'string') {
                            valueEl.className = 'string';
                            valueEl.textContent = `"${value}"`;
                        } else if (typeof value === 'number') {
                            valueEl.className = 'number';
                            valueEl.textContent = value;
                        } else if (typeof value === 'boolean') {
                            valueEl.className = 'boolean';
                            valueEl.textContent = value;
                        } else if (value === null) {
                            valueEl.className = 'null';
                            valueEl.textContent = 'null';
                        } else {
                            valueEl.textContent = value;
                        }
                        
                        itemEl.appendChild(valueEl);
                    }
                    
                    return itemEl;
                };
                
                jsonTreeEl.appendChild(renderItem(null, data));
            }
            
            // 初始渲染JSON树
            renderJsonTree(JSON.parse(sampleJson));
            
            // 更新行数
            lineCountEl.textContent = `${editor.getValue().split('\n').length} 行`;
            
            // 监听编辑器内容变化
            editor.onDidChangeModelContent(function() {
                const lineCount = editor.getValue().split('\n').length;
                lineCountEl.textContent = `${lineCount} 行`;
                
                try {
                    JSON.parse(editor.getValue());
                    statusEl.textContent = "状态: 就绪 (有效JSON)";
                } catch (error) {
                    statusEl.textContent = "状态: 就绪 (编辑中)";
                }
            });
        });
    </script>
</body>
</html>